<?php

/**
 * This is the model class for table "tbl_drug".
 *
 * The followings are the available columns in table 'tbl_drug':
 * @property integer $id
 * @property integer $id_type
 * @property integer $id_brand
 * @property string $name
 * @property string $description
 * @property integer $user_add_id
 * @property string $time_add
 * @property string $images
 *
 * The followings are the available model relations:
 * @property Users $userAdd
 * @property Typedrug $idType
 * @property Brand $idBrand
 * @property Store[] $tblStores
 */
class Drug extends DrugActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Drug the static model class
     */
    public $idStore =0;
    public $nameStore;
    public $addressStore;
    public $priceDrug;
    public $radio;
    public $check =0;
    public $hidden;
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    public function getIdTypeDrug() {
        $sql = Yii::app()->db->createCommand('select * from tbl_typedrug');
        $type = CHtml::listData($sql->queryAll(), 'id_type', 'name');
        return $type;
    }
    public function getIdStore() {
        $sql = Yii::app()->db->createCommand('select * from tbl_store');
        $type = CHtml::listData($sql->queryAll(), 'id_store', 'name');
        return $type;
    }
    public function getIdBrand() {
        $sql = Yii::app()->db->createCommand('select * from tbl_brand');
        $brand = CHtml::listData($sql->queryAll(), 'id_brand', 'name');
        return $brand;
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_drug';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id_type, id_brand, name, description, images,priceDrug', 'required'),
            array('id_type, id_brand, user_add_id,priceDrug', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 200),
            array('images', 'file', 'types' => 'jpg,png,gif', 'allowEmpty' => false),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, id_type, id_brand, name, description, user_add_id, time_add, images', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'userAdd' => array(self::BELONGS_TO, 'Users', 'user_add_id'),
            'idType' => array(self::BELONGS_TO, 'Typedrug', 'id_type'),
            'idBrand' => array(self::BELONGS_TO, 'Brand', 'id_brand'),
            'tblStores' => array(self::MANY_MANY, 'Store', 'tbl_drugstore(id_drug, id_store)'),
            'comments' => array(self::HAS_MANY, 'Comment', 'id_drug'),
            'commentCount' => array(self::STAT, 'Comment', 'id_drug'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'idStore'=>'Chọn cửa hàng',
            'id_type' => 'Kiểu thuốc',
            'id_brand' => 'Nhãn thuốc',
            'name' => 'Tên thuốc',
            'description' => 'Mô tả về thuốc',
            'priceDrug' => 'Giá thuốc',
            'nameStore' => 'Tên cửa hàng',
            'addressStore'=> 'Địa chỉ cửa hàng',
            'user_add_id' => 'Người dùng thêm',
            'time_add' => 'Thời gian thêm',
            'images' => 'Ảnh đại diện',
            'radio' =>'Bạn hãy chọn của hàng của mình?',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('id_type', $this->id_type);
        $criteria->compare('id_brand', $this->id_brand);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('user_add_id', $this->user_add_id);
        $criteria->compare('time_add', $this->time_add, true);
        $criteria->compare('images', $this->images, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function addComment($comment) {
        $comment->id_drug = $this->id;
        return $comment->save();
    }

}